package com.tim.library;

import android.annotation.SuppressLint;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.VibrationEffect;
import android.os.Vibrator;

import com.tencent.qcloud.tuicore.TUILogin;

public class SoundHelper {

    private static final int MAX_STREAMS = 2;
    private static final int DEFAULT_QUALITY = 0;

    private static final int DEFAULT_PRIORITY = 1;
    private static final int LEFT_VOLUME = 1;
    private static final int RIGHT_VOLUME = 1;
    private static final int LOOP = 0;
    private static final float RATE = 1.0f;

    /**
     * 音频的相关类
     */
    private SoundPool mSoundPool;
    private Vibrator mVibrator;

    private static class SoundHelperHolder {
        static final SoundHelper INSTANCE = new SoundHelper();
    }

    /**
     * 获取单例对象
     */
    public static SoundHelper getInstance() {
        return SoundHelper.SoundHelperHolder.INSTANCE;
    }

    private SoundHelper() {
        // 初始化行营的音频类
        initSoundPool();
        initVibrator();
    }

    /**
     * 初始化短音频的内容
     */
    private void initSoundPool() {
        // 根据不同的版本进行相应的创建
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mSoundPool = new SoundPool.Builder()
                    .setMaxStreams(MAX_STREAMS)
                    .setAudioAttributes(
                            new AudioAttributes
                                    .Builder()
                                    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                                    .build()
                    )
                    .build();
        } else {
            mSoundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_SYSTEM, DEFAULT_QUALITY);
        }
    }

    /**
     * 初始化震动的对象
     */
    private void initVibrator() {
        mVibrator = (Vibrator) TUILogin.getAppContext().getSystemService(Context.VIBRATOR_SERVICE);
    }

    /**
     * 开始播放音频
     */
    public void playSound(int rawID) {
        if (mSoundPool == null) initSoundPool();

        mSoundPool.load(
                TUILogin.getAppContext(),
                rawID,
                DEFAULT_PRIORITY
        );

        mSoundPool.setOnLoadCompleteListener(
                (soundPool, sampleId, status) ->
                        soundPool.play(
                                sampleId,
                                LEFT_VOLUME,
                                RIGHT_VOLUME,
                                DEFAULT_PRIORITY,
                                LOOP,
                                RATE
                        )
        );
    }

    /**
     * 开启震动
     * <p>
     * 注：当打开camera时，震动可能无效
     */
    @SuppressLint("MissingPermission")
    public void startVibrator(long milliseconds) {
        if (mVibrator == null) initVibrator();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mVibrator.vibrate(
                    VibrationEffect.createOneShot(
                            milliseconds,
                            VibrationEffect.DEFAULT_AMPLITUDE
                    )
            );
        } else {
            mVibrator.vibrate(1000);
        }
    }

    /**
     * 同时开始音乐和震动
     */
    @SuppressWarnings("unused")
    public void startSoundAndVibrator(int rawID, long milliseconds) {
        playSound(rawID);
        startVibrator(milliseconds);
    }

    /**
     * 释放相应的资源
     */
    @SuppressLint("MissingPermission")
    public void release() {
        // 释放所有的资源
        if (mSoundPool != null) {
            mSoundPool.release();
            mSoundPool = null;
        }

        if (mVibrator != null) {
            mVibrator.cancel();
            mVibrator = null;
        }
    }

}
